/*
 * PicFrame.java
 *
 * Created on 2011年1月5日, 下午1:00
 */
package dailyPic;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author  heantai
 */
public class PicFrame extends javax.swing.JFrame {

  DailyPic dp = new DailyPic();

  /** Creates new form PicFrame */
  public PicFrame() {
    initComponents();
    try {
      dp.getUnrenamedImgList();
      String picPath = dp.getPic();
      picName.setText(picPath);
      imgPanel.setImg(picPath);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    newName = new javax.swing.JTextField();
    nextPic = new javax.swing.JButton();
    rename = new javax.swing.JButton();
    imgPanel = new dailyPic.ImgPanel();
    picName = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        formWindowClosing(evt);
      }
    });

    nextPic.setText("下一张");
    nextPic.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        nextPicActionPerformed(evt);
      }
    });

    rename.setText("重命名");
    rename.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        renameActionPerformed(evt);
      }
    });

    imgPanel.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        imgPanelMouseClicked(evt);
      }
    });

    javax.swing.GroupLayout imgPanelLayout = new javax.swing.GroupLayout(imgPanel);
    imgPanel.setLayout(imgPanelLayout);
    imgPanelLayout.setHorizontalGroup(
      imgPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 595, Short.MAX_VALUE)
    );
    imgPanelLayout.setVerticalGroup(
      imgPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 334, Short.MAX_VALUE)
    );

    picName.setEditable(false);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(picName, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
          .addComponent(newName, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
          .addComponent(nextPic, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(rename, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)))
      .addComponent(imgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addComponent(imgPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(nextPic)
          .addComponent(picName, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(rename)
          .addComponent(newName, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

private void renameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameActionPerformed
// 重命名
  String name = newName.getText().trim();
  if (name != null && !"".equals(name)) {
    dp.rename(name);
  } else {
    JOptionPane.showMessageDialog(null, "请重新输入名字");
  }

}//GEN-LAST:event_renameActionPerformed

private void nextPicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextPicActionPerformed
  // 下一张
  try {
    String picPath = dp.getPic();
    picName.setText(picPath);
    imgPanel.setImg(picPath);
    this.repaint();
  } catch (Exception ex) {
    Logger.getLogger(PicFrame.class.getName()).log(Level.SEVERE, null, ex);
  }
}//GEN-LAST:event_nextPicActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
  try {
    dp.saveLog();
    System.exit(0);
    } catch (IOException ex) {//GEN-LAST:event_formWindowClosing
      Logger.getLogger(PicFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
private void imgPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgPanelMouseClicked
// TODO add your handling code here:
  // 下一张
  try {
    String picPath = dp.getPic();
    picName.setText(picPath);
    imgPanel.setImg(picPath);
    this.repaint();
  } catch (Exception ex) {
    Logger.getLogger(PicFrame.class.getName()).log(Level.SEVERE, null, ex);
  }
}//GEN-LAST:event_imgPanelMouseClicked

  /**
   * @param args the command line arguments
   */
  public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

      public void run() {
        new PicFrame().setVisible(true);

      }
    });
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private dailyPic.ImgPanel imgPanel;
  private javax.swing.JTextField newName;
  private javax.swing.JButton nextPic;
  private javax.swing.JTextField picName;
  private javax.swing.JButton rename;
  // End of variables declaration//GEN-END:variables
}
